package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.FameModel;
import com.zw.yzk.kabu.utils.CircleTransform;
import com.zw.yzk.kabu.utils.PhoneUtils;
import com.zw.yzk.kabu.view.activity.HomePageActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/12.
 */

public class FameListAdapter extends RecyclerView.Adapter<FameListAdapter.Holder> {

    private Context context;
    private List<FameModel> dataList;

    public FameListAdapter(List<FameModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void refresh(List<FameModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void add(List<FameModel> list) {
        if (list != null) {
            int start = dataList.size();
            dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View rootView = LayoutInflater.from(context).inflate(R.layout.recycler_item_fame_list, parent, false);
        return new Holder(rootView);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final FameModel model = dataList.get(position);
//        switch (position) {
//            case 0:
//                holder.range.setBackgroundResource(R.drawable.ic_fame_first);
//                holder.range.setText("");
//                break;
//            case 1:
//                holder.range.setBackgroundResource(R.drawable.ic_fame_second);
//                holder.range.setText("");
//                break;
//            case 2:
//                holder.range.setBackgroundResource(R.drawable.ic_fame_third);
//                holder.range.setText("");
//                break;
//            default:
//                holder.range.setBackgroundResource(0);
//                holder.range.setText(String.valueOf(position + 1));
//                break;
//        }
        if (model.getRanking() <= 1000) {
            holder.range.setBackgroundResource(R.drawable.ic_ranking_gold);
        } else if (model.getRanking() <= 5000) {
            holder.range.setBackgroundResource(R.drawable.ic_ranking_sliver);
        } else {
            holder.range.setBackgroundResource(R.drawable.ic_ranking_bronze);
        }
        holder.range.setText(String.valueOf(model.getRanking()));

        holder.name.setText(model.getNickName());
        holder.nickName.setText(model.getNickName());
        holder.location.setText(model.getCity());
        holder.location.setVisibility(TextUtils.isEmpty(model.getCity()) ? View.GONE : View.VISIBLE);
        holder.fans1.setText(String.valueOf(model.getFansCount()));
        holder.fans2.setText(String.valueOf(model.getFansCount()));
        holder.address.setText(model.getProvince());
        holder.introduceText.setText(model.getIntroduceText());

        Glide.with(context)
                .load(model.getHeadUrl())
                .centerCrop()
                .error(R.drawable.ic_default_head)
                .placeholder(R.drawable.ic_default_head)
                .transform(new CircleTransform(context))
                .into(holder.head);

        //按照图片比例将图片宽放大/缩小到屏幕宽度
        Glide.with(context)
                .load(model.getIntroduceUrl())
                .asBitmap()
                .error(R.drawable.im_default)
                .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        int imageWidth = resource.getWidth();
                        int imageHeight = resource.getHeight();
                        int screenWidth = PhoneUtils.getScreenWidth(context);
                        float scale = ((float) screenWidth) / imageWidth;
                        int height = (int) (scale * imageHeight);

                        ViewGroup.LayoutParams params = holder.introduceImage.getLayoutParams();
                        if (params == null) {
                            params = new ViewGroup.LayoutParams(screenWidth, height);
                        } else {
                            params.height = height;
                            params.width = screenWidth;
                        }
                        holder.introduceImage.setLayoutParams(params);
                        holder.introduceImage.setImageBitmap(resource);
                    }
                });

        holder.head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomePageActivity.class);
                intent.putExtra(ActivityConstant.HOME_PAGE_ID, model.getId());
                context.startActivity(intent);
            }
        });

        holder.baseInfoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.introduceLayout.getVisibility() != View.VISIBLE) {
                    holder.introduceLayout.setVisibility(View.VISIBLE);
                } else {
                    holder.introduceLayout.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.base_info_layout)
        View baseInfoLayout;
        @Bind(R.id.range)
        TextView range;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.location)
        TextView location;
        @Bind(R.id.fans_count_1)
        TextView fans1;
        @Bind(R.id.head)
        ImageView head;
        @Bind(R.id.introduce_layout)
        View introduceLayout;
        @Bind(R.id.introduce_image)
        ImageView introduceImage;
        @Bind(R.id.nick_name)
        TextView nickName;
        @Bind(R.id.fans_count_2)
        TextView fans2;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.introduce_text)
        TextView introduceText;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
